# 数据库主要用来查询参数，数据清理
import pymysql
# from common.read_config import Environ0
from common.read_yaml import get_yaml_filepath
configs_data = get_yaml_filepath(Environ='data.yaml')
db = configs_data['db']
print(db)
def get_db_conn():
    conn = pymysql.connect(host=db['host'],   # 数据库地址
                           port=db['port'],     # 端口
                           user=db['user_name'],  # 账号
                           passwd=db['password'],  # 密码
                           database=db['db_name'],  # 要操作的数据库名
                           charset='utf8')  # 指定编码格式
    return conn

def query_db(sql):
    conn = get_db_conn()  # 获取连接
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 建立游标
    cur.execute(sql)  # 执行SQL
    conn.commit()
    result = cur.fetchone()  # 获取查询结果
    print(result)
    cur.close()  # 关闭游标
    conn.close()  # 关闭连接
    return result

def delete_db(sql):
    conn = get_db_conn()  # 获取连接
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 建立游标
    cur.execute(sql)  # 执行SQL
    conn.commit()
    result = cur.fetchone()  # 获取查询结果
    print(result)
    cur.close()  # 关闭游标
    conn.close()  # 关闭连接
    return result

if __name__ == '__main__':
    query_db(sql="SELECT * FROM `jl_custom_user` WHERE `mobile_phone` = '13817810356' LIMIT 0, 1000")
